package zyh.JavaDev;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.ReplicateScaleFilter;
import java.lang.Thread.State;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;


public class Tetris extends Frame{
	private int[][] map=new int[21][14];//表示整个下落区域,值为2表示墙，值为1表示已经有方块，值为-1表示没有方块
	private int[][][] block ={
			{
				{0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0},               
				{0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0},
				{0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0},
				{0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0}                 
			},
			{
				{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
				{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0},
				{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
				{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0}
			},
			{
				{0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0},
				{0,0,0,0,0,1,1,1,0,1,0,0,0,0,0,0},
				{0,0,0,0,0,1,1,0,0,0,1,0,0,0,1,0},
				{0,0,0,0,0,0,1,0,1,1,1,0,0,0,0,0}
			},
			{
				{0,0,1,0,0,0,1,0,0,1,1,0,0,0,0,0},
				{0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,0},
				{0,0,0,0,0,1,1,0,0,1,0,0,0,1,0,0},
				{0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0}
			},
			{
				{0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,0},
				{0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0},
				{0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,0},
				{0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0}
			},
			{
				{0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0},
				{0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0},
				{0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0},
				{0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0}
			},
			{
				{0,0,0,0,0,1,0,0,1,1,1,0,0,0,0,0},
				{0,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0},
				{0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0},
				{0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0}
			}
	};
	private int blockType=(int)(Math.random()%7);//随机出方块的形状，总共有七种
	private int turnTpye =(int)(Math.random()%4);//随机出方块的变化位置，总共有四种
	private Button startButton=new Button();
	private Button pauseButton=new Button();
	private Button stopButton=new Button();
	private Timer timer=new Timer();
	private MyTask myTask=new MyTask();
	private int posX=0;//下落方块的x坐标
	private int posY=2;//下落方块的y坐标
	private int lastX=0;
	private int lastY=2;
	
	public Tetris()
	{
		SetFence();
		Drop(0, 0);

		this.setTitle("RyanTetris");
		this.setSize(400,480);
		this.setVisible(true);
		this.setResizable(false);
		
		this.setLayout(null);//在特定位置添加button，先set layout为null
		startButton.setLocation(315, 300);
		startButton.setSize(60, 30);
		startButton.setLabel("Start");
		pauseButton.setLocation(315, 340);
		pauseButton.setSize(60, 30);
		pauseButton.setLabel("Pause");
		stopButton.setLocation(315, 380);
		stopButton.setSize(60, 30);
		stopButton.setLabel("Quit");
		//设置退出
		stopButton.addMouseListener(new MouseListener() {
			
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
		this.add(startButton);
		this.add(pauseButton);
		this.add(stopButton);
		
		this.setFocusable(true);//响应按键有效
		this.addKeyListener(new keylistener());
		timer.schedule(myTask, 0,1000);
	}
	
	//画图
	public void paint(Graphics g)
	{
		//画围墙
		for(int i=0;i<21;i++)
			for(int j=0;j<14;j++)
			{
				if(map[i][j]==2)
				{
					g.setColor(Color.black);
					g.draw3DRect(j*20+10, i*20+30, 20, 20, true);
				}
			}
		
		//画得分及下一个方块
		g.drawString("Next One", 315, 40);
		g.drawString("Score", 320, 160);
		
		for(int i=0;i<21;i++)
			for(int j=0;j<14;j++)
			{
				if(map[i][j]==1)
				{
					g.fill3DRect(j*20+10, i*20+30, 20, 20, true);
				}
			}
		for(int i=0;i<21;i++)
			for(int j=0;j<14;j++)
			{
				if(map[i][j]==-1)
				{
					g.clearRect(j*20+10, i*20+30, 20, 20);
					
				}
			}
	}
	
	//设置围墙
	public void SetFence()
	{
		for(int i=0;i<21;i++)
		{
			map[i][0]=2;
			map[i][13]=2;
		}
		
		for(int i=0;i<14;i++)
		{
			map[20][i]=2;
		}
	}
	
	//方块下落
	public void Drop(int blocktype,int turntype)
	{
		int j=0;
		for(int i=0;i<16;i++)
		{
			if(block[blocktype][turntype][i]==1)
			{
				if(i%4!=0)
					map[j][i]=1;
				else 
				{
					j++;
					map[j][i%4]=1;
				}
			}
		}
		repaint();
	}
	
	//方块右移
	public void Rigth(int px,int py)
	{
		
		posY++;
		repaint();
	}
	
	//方块左移
	public void Left(int px,int py)
	{
		posY--;
		repaint();
	}
	//方块旋转
	public void Change()
	{
		
	}
	//解决repaint产生的闪烁问题
	public void update(Graphics g)
	{
		paint(g);
	}
		
	class MyTask extends TimerTask
	{

		public void run() {
			//Drop(1, 0);
		}	
	}
	
	//响应按键
	class keylistener implements KeyListener
	{

		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}

		public void keyPressed(KeyEvent e) {
			// TODO Auto-generated method stub
			switch (e.getKeyCode()) {
			case KeyEvent.VK_DOWN:
				Drop(posX, posY);
				break;
			case KeyEvent.VK_UP:
				Change();
				break;
			case KeyEvent.VK_LEFT:
				Left(posX, posY);
				break;
			case KeyEvent.VK_RIGHT:
				Rigth(posX, posY);
				break;
			default:
				break;
			}
		}

		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}
	//主函数
	public static void main(String args[])
	{
		Tetris MyTetris=new Tetris();
	}
}

